# -*- coding: utf-8 -*-
from db.mysql import MysqlClient
from common.config import *
from common.helper import Helper
from itertools import product


class Spider(object):
    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "如E航空保"

    # 生成产品信息
    def genProductInfo(self):
        plan = {

            "days": ["7d", "15d", "1m", "1y"],
            "object": [
                {"airplane_death": "1000000"},
                {"airplane_death": "2000000"},
                {"airplane_death": "5000000"},
                {"airplane_death": "10000000"},
            ]
        }

        prices = ["3.5", "7", "18", "35", "7", "15", "35", "70", "7", "15", "35", "70", "70", "140", "350", "700"]
        # 计数变量
        num = 0
        for v in product(plan["days"], plan["object"]):
            data = {
                "name": self.name,
                "company": "中国人寿",
                "days": v[0],
                "people_count": "1",
                "price": Helper.decimalParse(prices[num])
            }

            num += 1

            data.update(v[1])
            # 执行合并
            self.mergeTable(data)

    # 合并数据库
    def mergeTable(self, data):
        SQL = 'INSERT INTO `' + TABLE_ACCIDENT + '`(`name`,`company`,`days`,`people_count`,`price`,`airplane_death`)VALUES("{name}","{company}","{days}","{people_count}","{price}","{airplane_death}")'

        print(SQL.format(**data))

        self.mysql.cursor.execute(SQL.format(**data))
        self.mysql._conn.commit()


if __name__ == '__main__':
    a = Arrangement()
    a.genProductInfo()
